package com.ddwl.user.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.user.*;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.user.RoleVo;
import com.ddwl.user.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Fu Zhaohui
 * @version 1.0.0
 * @date 2019/8/19 16:49
 */
@Api(tags = "角色服务")
@RequestMapping("/role")
@Auth(product = BaseEnum.ProductEnum.CONSOLE)
@RestController
public class RoleController extends BaseController {
    @Autowired
    private RoleService roleService;

    @PostMapping("/create")
    @ApiOperation("创建角色")
    public RestVo create(@Valid @RequestBody RoleAddBo bo) {
        return RestVo.ok(roleService.create(bo));
    }

    @PostMapping("/update")
    @ApiOperation("修改角色")
    public RestVo update(@Valid @RequestBody RoleEditBo bo) {
        roleService.update(bo);
        return RestVo.ok();
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除角色")
    public RestVo delete(@RequestParam String id) {
        roleService.delete(id);
        return RestVo.ok();
    }

    @GetMapping("/detail")
    @ApiOperation("获取角色明细")
    public RestVo<RoleVo> detail() {
        return RestVo.ok(roleService.getById(getUserId()));
    }


    @PostMapping("/page")
    @ApiOperation("分页查询角色")
    public RestVo<PageVo<RoleVo>> page(@ApiParam(value = "分页Bo", required = true) @Valid @RequestBody PageBo<RoleQueryBo> pageBo) {
        return RestVo.ok(roleService.page(pageBo));
    }


    @ApiOperation("查询角色关联的权限")
    @GetMapping("/roel_menus")
    public RestVo<List<String>> getRoleMenus(@ApiParam(value = "角色ID",required = true) @RequestParam String id){
        return RestVo.ok(roleService.getRoleMenu(id));
    }

    @ApiOperation("设置角色权限")
    @PostMapping("/allot_menus")
    public RestVo setMenus(@Valid @RequestBody RoleAllotMenuBo bo){
        roleService.allotMenu(bo);
        return RestVo.ok();
    }


    @ApiOperation("查询用户关联角色")
    @GetMapping("/user_role")
    public RestVo<List<String>> getUserRole(@ApiParam(value = "用户ID",required = true) @RequestParam String id){
        return RestVo.ok(roleService.getUserRole(id));
    }

    @ApiOperation("设置用户角色")
    @PostMapping("/allot_user")
    public RestVo setUser(@Valid @RequestBody UserAllotRoleBo bo){
        roleService.allotRole(bo);
        return RestVo.ok();
    }

    @PostMapping("/changeId")
    @ApiOperation("把id反转成时间戳")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo changeId(){
        roleService.changeId();
        return RestVo.ok();
    }


}
